package com.abu.edums.service.impl;

import com.abu.edums.mapper.ChapterMapper;
import com.abu.edums.mapper.CourseDescriptionMapper;
import com.abu.edums.mapper.VideoMapper;
import com.abu.pojo.*;
import com.abu.edums.mapper.CourseMapper;
import com.abu.vo.CourseInfoVO;
import com.abu.vo.CoursePublishVo;
import com.abu.edums.service.CourseService;
import com.abu.exceptionhandler.EduException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 阿布
 * @since 2020-06-18
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseDescriptionMapper courseDescriptionMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private ChapterMapper chapterMapper;


    @Override
    public String saveCourseInfo(CourseInfoVO courseInfoVO) {
//        添加课程
        boolean flag = this.save(courseInfoVO);
//        获取添加课程的id
        if (!flag) {
            throw new EduException(20001, "添加课程失败");
        }
        String courseInfoVOid = courseInfoVO.getId();
//        添加课程描述
        CourseDescription description = new CourseDescription();
//        设置description时须将id设置为手动赋值（input）
        description.setId(courseInfoVOid).setDescription(courseInfoVO.getDescription());
        int count = this.courseDescriptionMapper.insert(description);
        if (count != 1) {
            throw new EduException(20001, "添加描述信息失败");
        }
        return courseInfoVO.getId();
    }

    @Override
    public String updateCourseInfoVO(CourseInfoVO courseInfoVO) {
//        根据id更新课程
        boolean flag = this.updateById(courseInfoVO);
//        获取课程的id
        String id = courseInfoVO.getId();
        if (!flag) {
            throw new EduException(20001, "添加课程失败");
        }
//        更新课程描述信息
        CourseDescription description = new CourseDescription();
        description.setId(id).setDescription(courseInfoVO.getDescription());
        int count = this.courseDescriptionMapper.updateById(description);
        if (count != 1) {
            throw new EduException(20001, "添加描述信息失败");
        }
        return id;
    }

    @Override
    public CoursePublishVo getCoursePublishInfo(String courseId) {
        return this.courseMapper.queryAllPublishCourse(courseId);
    }


    @Override
    @Transactional
    public Boolean deletePublishCourseById(String courseId) {
//       先查询出vodId
        Course course = this.courseMapper.selectById(courseId);
//        1.删小节和存储在阿里云的视频资源
        this.videoMapper.delete(new QueryWrapper<Video>().eq("course_id", courseId));
//        2.删章节
        this.chapterMapper.delete(new QueryWrapper<Chapter>().eq("course_id", courseId));
//        3.删描述
        this.courseDescriptionMapper.deleteById(courseId);
//        4.删除发布的课程
        int count = this.courseMapper.deleteById(courseId);
        if (count != 1) {
            throw new EduException(20001, "删除发布的课程失败");
        }
        return true;
    }

    @Override
    public List<Course> queryLimitById() {
//        1.先设置查询条件
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
//        2.设置查询个数
        Page<Course> page = new Page<>();
        page.setSize(8);
//        3.执行查询
        Page<Course> coursePage = this.courseMapper.selectPage(page, wrapper);
//        4.返回结果集
        return coursePage.getRecords();
    }

    @Override
    public HashMap<String, Object> queryPageCourse(Long page, Long size, CourseQuery courseQuery) {
//        1.构造分页条件
        Page<Course> coursePage = new Page<>(page, size);
//        2.构造查询条件
        String status = courseQuery.getStatus();
        String queryTitle = courseQuery.getTitle();
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
//        条件非空查询
        if (StringUtils.isNotBlank(status)) {
            wrapper.eq("status", status);
        }
        if (StringUtils.isNotBlank(queryTitle)) {
            wrapper.like("title", queryTitle);
        }
//        3.执行查询
        Page<Course> coursePageResult = this.page(coursePage, wrapper);
//        4.返回结果集
        List<Course> courseCurrentList = coursePageResult.getRecords();
        long total = coursePageResult.getTotal();
//        5.封装结果集
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", courseCurrentList);
        map.put("total", total);
        return map;
    }

}
